package MyBatis;

import MyBatis.model.User;
import MyBatis.model.UserState;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.Reader;
import java.util.List;

/**
 * @Fou Jack of all trades and master of none.
 * @date 2022/1/7 10:37
 */
public class BasicDemo {
    public static void main(String[] args) throws IOException {
    String resource = "mybatis-config.xml";
    Reader reader = Resources.getResourceAsReader(resource);
    SqlSessionFactory sessionFactory =new SqlSessionFactoryBuilder().build(reader);
        SqlSession session = sessionFactory.openSession();
        String statement = "MyBatis.maper.UserMapper.getUserList";
        List<User> users = session.selectList(statement);
        for (User u:users){
            System.out.println(u.toString());
        }
    }
    @Test
    public void TypeHandler() throws IOException {
        String res = "mybatis-config.xml";
        Reader rea = Resources.getResourceAsReader(res);
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(rea);
        SqlSession session = sessionFactory.openSession();
        String Stmt = "MyBatis.maper.UserMapper.addUser";
        User user = new User("学习Java的小学生",21,980, UserState.DISABLED);
        session.insert(Stmt,user);
        session.commit();
        System.out.println(user.getId());
        Stmt = "MyBatis.maper.UserMapper.getUser";
        user = session.selectOne(Stmt,user.getId());
        System.out.println(user.toString());
    }

}
